#include "PackedStringAllocator.h"

#include <core/allocator/allocator_pool_manager.h>
#include <core/singleton.h>

char* PackedStringAllocator::GetNullString() {
  static char blank[2] = {0, 0};
  return blank;
}
bool PackedStringAllocator::LoadPackedString(int category, int id, int size,
                                             char*& ptr) {
  return false;
}
int PackedStringAllocator::SavePackedString(int category, const char* str,
                                            int size, const char* str2,
                                            int size2) {
  return 1;
}

namespace {
class PackedStringAllocatorImpl
    : public qb::base::Singleton<PackedStringAllocatorImpl> {
  qb::base::HeapAllocator m_heap;

 public:
  PackedStringAllocatorImpl() : m_heap("PackedString") {}
  char* Alloc(int size) { return (char*)m_heap.Alloc(size); }
  void Free(void* ptr) {
    int size = ptr ? (strlen((const char*)ptr) + 1) : 0;
    m_heap.Free(ptr, size);
  }
  int GetMemory() const { return m_heap.GetBytes(); }
};

}  // namespace
char* PackedStringAllocator::Alloc(int size) {
  return PackedStringAllocatorImpl::instance()->Alloc(size);
}
void PackedStringAllocator::Free(void* ptr) {
  PackedStringAllocatorImpl::instance()->Free(ptr);
}
int PackedStringAllocator::GetMemory() {
  return PackedStringAllocatorImpl::instance()->GetMemory();
}
